package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.builder.vo.ExhibitorVO;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.home.dao.ReportEarnestMoneyAuditMapper;
import com.eastfair.home.dto.ReportEarnestMoneyAuditDTO;
import com.eastfair.home.dto.ReportSpaceAuditDTO;
import com.eastfair.home.entity.ReportEarnestMoneyAudit;
import com.eastfair.home.entity.ReportSpaceAudit;
import com.eastfair.home.service.HomeExhibitorService;
import com.eastfair.home.service.ReportEarnestMoneyAuditHistoryService;
import com.eastfair.home.service.ReportEarnestMoneyAuditService;
import com.eastfair.home.vo.ReportEarnestMoneyAuditVO;
import com.eastfair.venueservice.consts.CommonConstants;
import com.eastfair.venueservice.enumeration.ServiceItemAuditStateEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * <p>
 * 业务实现类
 * 报馆保证金审核记录
 * </p>
 *
 * @author linan
 * @date 2023-04-17
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportEarnestMoneyAuditServiceImpl extends SuperServiceImpl<ReportEarnestMoneyAuditMapper, ReportEarnestMoneyAudit> implements ReportEarnestMoneyAuditService {

    @Resource
    private HomeExhibitorService homeExhibitorService;

    @Resource
    private ReportEarnestMoneyAuditHistoryService reportEarnestMoneyAuditHistoryService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportEarnestMoneyAudit> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportEarnestMoneyAudit model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public ReportEarnestMoneyAuditVO queryReportEarnestMoneyAudit(ReportEarnestMoneyAuditDTO reportEarnestMoneyAuditDTO) {
        ReportEarnestMoneyAudit entity = getOne(new QueryWrapper<ReportEarnestMoneyAudit>().lambda()
                .eq(ReportEarnestMoneyAudit::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(ReportEarnestMoneyAudit::getReportSpaceId, reportEarnestMoneyAuditDTO.getReportSpaceId()));
        return BeanUtil.copyProperties(entity, ReportEarnestMoneyAuditVO.class);
    }

    @Override
    public ReportEarnestMoneyAudit getByReportSpaceId(Long reportSpaceId) {
        return this.getOne(
                new QueryWrapper<ReportEarnestMoneyAudit>()
                        .lambda()
                        .eq(ReportEarnestMoneyAudit::getIsDeleted, BusinessConstant.DELETE_NO)
                        .eq(ReportEarnestMoneyAudit::getReportSpaceId, reportSpaceId)
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveReportEarnestMoneyAudit(ReportEarnestMoneyAuditDTO reportEarnestMoneyAuditDTO) {
        ReportEarnestMoneyAudit reportEarnestMoneyAudit = BeanUtil.toBean(reportEarnestMoneyAuditDTO, ReportEarnestMoneyAudit.class);
        return save(reportEarnestMoneyAudit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitReportEarnestMoneyAudit(ReportSpaceAuditDTO reportSpaceAuditDTO, ReportSpaceAudit reportSpaceAudit) {
        // 报馆ID移除保证金审核记录
        removeByReportSpaceId(reportSpaceAudit.getReportSpaceId());

        // 保存保证金审核记录
        ReportEarnestMoneyAuditDTO reportEarnestMoneyAuditDTO = BeanUtil.copyProperties(reportSpaceAudit, ReportEarnestMoneyAuditDTO.class, CommonConstants.COMMON_IGNORE_PROPERTIES);

        ExhibitorVO exhibitor = homeExhibitorService.getExhibitor(reportSpaceAuditDTO.getExhibitorId());
        reportEarnestMoneyAuditDTO.setIsDouble(exhibitor.getIsDouble());
        reportEarnestMoneyAuditDTO.setIsPoint(exhibitor.getIsPoint());
        reportEarnestMoneyAuditDTO.setExhibitionManageId(exhibitor.getExhibitionManageId());

        reportEarnestMoneyAuditDTO.setReportSpaceAuditId(reportSpaceAudit.getId());
        reportEarnestMoneyAuditDTO.setEarnestMoney(reportSpaceAuditDTO.getEarnestMoney());
        reportEarnestMoneyAuditDTO.setAuditState(ServiceItemAuditStateEnum.NOT_APPROVED.getCode());
        saveReportEarnestMoneyAudit(reportEarnestMoneyAuditDTO);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReportEarnestMoneyAudit updateEarnestMoneyAuditStatus(ReportSpaceAuditDTO reportSpaceAuditDTO) {
        ReportEarnestMoneyAudit earnestMoneyAudit = getByReportSpaceId(reportSpaceAuditDTO.getReportSpaceId());
        earnestMoneyAudit.setEarnestMoney(reportSpaceAuditDTO.getEarnestMoney());
        earnestMoneyAudit.setAuditState(reportSpaceAuditDTO.getEarnestMoneyAuditStatus().getCode());
        earnestMoneyAudit.setAuditOpinion(reportSpaceAuditDTO.getEarnestMoneyAuditOpinion());
        updateById(earnestMoneyAudit);
        // 插入审核历史
        reportEarnestMoneyAuditHistoryService.asyncSaveHistoryByReportEarnestMoneyAudit(earnestMoneyAudit);
        return earnestMoneyAudit;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByReportSpaceId(Long reportSpaceId) {
        log.info("removeByReportSpaceId - 报馆ID删除保证金审核记录, reportSpaceId={}", reportSpaceId);
        return this.update(new UpdateWrapper<ReportEarnestMoneyAudit>()
                .lambda()
                .eq(ReportEarnestMoneyAudit::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(ReportEarnestMoneyAudit::getReportSpaceId, reportSpaceId)
                .set(ReportEarnestMoneyAudit::getIsDeleted, BusinessConstant.YES));
    }
}
